﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace Battleship
{
    public class Draw
    {
        Graphics formGraphic;
        int rootX;
        int rootY;
        int containerW;
        int containerH;
        int bayD;
        int rowD;
        int tierD;

        public Draw(Graphics formG)
        {
            formGraphic = formG;
            rootX = 200;
            rootY = 20;
            bayD = 10;
            rowD = 14;
            tierD = 140;
            containerH = 17;
            containerW = 116;
        }
        public void drawRetangle(int x, int y, int w, int h, Color color)
        {
            System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(color);
            System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Gray, 3);
            this.formGraphic.FillRectangle(myBrush, new Rectangle(x, y, w, h));
        }
        public void drawContainer(int x, int y, ContainerInfo container, int mode)
        {
            String info = "";
            switch (mode)
            {
                case 1:
                    info += "[" + container.no + "]\t" + " " + container.Size + "\t" + container.Type;
                    break;
                case 2:
                    info += "[" + container.no + "]\t" + Math.Round(container.Weight);
                    break;
                case 3:
                    info += "G" + container.GCor.getStr() + "L" + container.LCor.getStr();
                    break;
                case 4:
                    info += "[" + container.no + "]\t";
                    break;
                default:
                    break;
            }

            // BG
            Color color;
            switch (container.Type)
            {
                case "ORD":
                    color = Color.LightBlue;
                    break;
                case "EMP":
                    color = Color.LightGreen;
                    break;
                case "DAN":
                    color = Color.LightCoral;
                    break;
                case "REF":
                    color = Color.LightSeaGreen;
                    break;
                default:
                    color = Color.White;
                    break;
            }


            // draw BG
            drawRetangle(x, y, containerW, containerH, color);
            Font font = new Font("Calibri", 8);
            SolidBrush brush = new SolidBrush(Color.Black);
            PointF point = new PointF(x, y);

            // draw port
            Color colorPort;
            switch (container.POD.PI)
            {
                case 0:
                    colorPort = Color.Chocolate;
                    break;
                case 1:
                    colorPort = Color.Silver;
                    break;
                default:
                    colorPort = Color.Blue;
                    break;
            }
            drawRetangle(x, y - 6, 6, 6, colorPort);

            // draw string
            this.formGraphic.DrawString(info, font, brush, point);
        }
        public void drawContainerSlot(ContainerSlot containerSlot, int mode)
        {
            int tier = containerSlot.location.tier;
            int bay = containerSlot.location.bay;
            int row = containerSlot.location.row;

            int x = bay * (bayD + containerW) + rootX;
            int y = tier * tierD + row * (rowD + containerH) + rootY;

            ContainerInfo container = containerSlot.container;
            Color color = Color.LightGray;

            // draw index
            Font font = new Font("Calibri", 6);
            SolidBrush brush = new SolidBrush(Color.Black);
            PointF point = new PointF(x + containerW - 20, y - containerH / 2 - 3);
            string info = containerSlot.location.getStr();
            this.formGraphic.DrawString(info, font, brush, point);



            // draw slot
            drawRetangle(x - 2, y - 2, containerW + 4, containerH + 4, Color.White);

            // draw pType
            point = new PointF(x, y);
            font = new Font("Calibri", 10);
            this.formGraphic.DrawString(containerSlot.pType.ToString(), font, brush, point);

            if (container != null)
            {

                // draw connector for 40ft
                if (container.Size == 40 && (containerSlot.location.bay % 2) == 0)
                {
                    drawRetangle(x + containerW, y, bayD, containerH, Color.Black);
                }

                // draw content
                drawContainer(x, y, container, mode);



            }

            // draw moment
            point = new PointF(x + 10, y - containerH / 2 - 4);
            font = new Font("Calibri", 7);
            this.formGraphic.DrawString(containerSlot.valMoment().getStr(), font, brush, point);
        }
        public void drawLoadingSpace(LoadingSpace loadingSpace, int mode)
        {
            this.formGraphic.Clear(Color.WhiteSmoke);
            int numT = loadingSpace.numTier;
            int numB = loadingSpace.numBay;
            int numR = loadingSpace.numRow;

            for (int tier = 0; tier < numT; tier++)
            {
                for (int bay = 0; bay < numB; bay++)
                {
                    for (int row = 0; row < numR; row++)
                    {
                        drawContainerSlot(loadingSpace.content[tier, bay, row], mode);
                    }
                }
            }
        }
    }
}
